import {mapState} from 'vuex'

const mixin = {
  data() {
    return {
      backObjName: this.$options.name,
      scrollY: 0,
      scrollX: 0,
      pageTimeoutTime: null,
      isOnPage: false   // 是否停留在当前页面.
    }
  },
  computed: {
    ...mapState({
      hs: 'hs',
      bridge: 'bridge',
      user: 'user',
      domain: 'domain'
    })
  },
  methods: {
    /**
     * x or y的滚动距离
     * @param pos
     */
    scroll(pos) {
      this.scrollX = pos.x
      this.scrollY = pos.y
    },
    /**
     * 滚动到指定目标
     * @param targetIndex
     */
    scrollTo(targetIndex) {
      console.log('scrollTo:' + targetIndex);
      this.$refs.scrollContainer.scrollTo(targetIndex);
    },
    /**
     * 设置返回焦点
     * @param backObj
     */
    setBackObj(backObj) {
      if (this.backObjName) this.hs.cookie.set(this.backObjName, JSON.stringify(backObj));
    },
    /**
     * 获取记忆焦点
     * @param focStatus
     * @returns {Array}
     */
    execBackFocObjPage(focStatus) {
      let backObj = {}
      let focArr = []
      if (this.hs.cookie.get(this.backObjName)) {
        backObj = JSON.parse(this.hs.cookie.get(this.backObjName));
        focArr = backObj.backFocArr;
        this.hs.cookie.delOne(this.backObjName);
        focArr.forEach((item, index) => {
          this.hs.initFocus(item, focStatus[index]);
        })
      }
      return focArr
    },
    /**
     * 自家后台栏目点击
     * @param id
     */
    columnClick(id) {
      // 埋点
      this.hs.ajax.get(
        "http://" + this.$store.state.domain + "/SMSCH-STB/column/click?columnId=" + id,
        data => {
        }, 0, function (msg) {
        }, 3000);
    },
    /**
     * 自家后台导航点击
     * @param id
     */
    navigationClick(id) {
      // 埋点
      this.hs.ajax.get(
        "http://" + this.$store.state.domain + "/SMSCH-STB/column/click?navigationId=" + id,
        data => {
        }, 0, function (msg) {
        }, 3000);
    },
    /**
     * 自家后台素材点击
     * @param id
     */
    materialClick(id) {
      // 埋点
      this.hs.ajax.get(
        "http://" + this.$store.state.domain + "/SMSCH-STB/material/click?materialId=" + id,
        data => {
        }, 0, function (msg) {
        }, 3000);
    },
    /**
     * 华数水晶球数据收集
     * @param cp
     * @param cpcode
     */
    wasuLog(cp, cpcode) {
      // 当通过bridge不能获取机顶盒时间的时候, 从自己服务器获取时间. (备选方案)
      // let {data} = await this.$axios.get('http://125.210.113.145/WSL/Core/DateFormat.ashx?style=40');
      // console.log(data);

      this.bridge.getStbidTime(async (longtime) => {
        try {
          longtime = this.$options.filters.datefmt(longtime, 'YYYY-MM-DD HH:mm:ss:SSS')
          let {data} = await this.$axios.post("http://125.210.113.145/WSL/Core/CrossDomain.ashx",
            "url=" + encodeURIComponent('http://ilog.wasu.cn/iLog/log') +
            "&data=" + encodeURIComponent(this.$qs.stringify({
              stbid: this.bridge.stbid,
              customerid: 'userprofile',
              dept: 0,
              projectName: '4k',
              pos: 0,
              time: longtime,
              cp: cp,
              cpcode: cpcode,
            })) +
            "&method=get&type=default")
          console.log(data);
        } catch (e) {
          console.log(e);
        }
      })
    }
  },
  created() {
    // 页面停留1秒,设为停留在当前页面
    this.pageTimeoutTime = setTimeout(() => {
      this.isOnPage = true
      this.pageTimeoutTime = null
    }, 1000)
  },
  mounted() {

  },
  beforeDestroy() {
    // 销毁定时器
    if (this.pageTimeoutTime) clearTimeout(this.pageTimeoutTime)
  }
}
export default mixin

